home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Peter Lewis / PNL Libraries / MyNotifier.p < prev    next >
Encoding:
Text File  |  1994-08-04  |  5.3 KB  |  224 lines  |  [TEXT/PJMM]

  1. unit MyNotifier;
  2.  
  3. { Derived from <jholt@adobe.COM> Joe Holt's StartupError code as posted }
  4. { to comp.sys.mac.programmer in May 1991 }
  5.  
  6. { Notification Manager messages }
  7.  
  8. { History: }
  9. {   jhh 18 jun 90 -- response to news posting }
  10. {   pnl 29 may 91 -- Converted to pascal to be used in an application }
  11.  
  12. interface
  13.  
  14.     const
  15.         mark_app = 1;
  16.         mark_none = 0;
  17.  
  18.     procedure InitNotify;
  19.     procedure FinishNotify;
  20.     procedure NotifyH (mark: integer; sound: handle; sicn: handle; str: stringPtr; display_time: longInt);
  21.     procedure Notify (mark, sound: boolean; sicn_id, sicn_index, str_id, str_index: integer; display_time: longInt);
  22. { mark - mark the current application }
  23. { sound - play sysbeep }
  24. { sicn_id, sicn_index - SICN id to rotate with the apple & index (<1 -> 1)   OR 0&0 for no sicn }
  25. { str_id, str_index - STR# id & index    OR    STR id & 0    OR    0 & 0 }
  26.     procedure NotifyIdle (foreground: boolean);
  27. { Call this in the event loop with the boolean true if you are in the foreground to remove apple mark and flash }
  28.     procedure UnNotify;
  29. { Call this to get rid of the notification }
  30.  
  31.     var
  32.         notify_finished, notify_outstanding: boolean;
  33.         time_to_unnotify: longInt;
  34.  
  35. implementation
  36.  
  37.     uses
  38.         Notification, IconFamilies;
  39.  
  40.     const
  41.         sicn_size = 32;
  42.         T_NMInstall = $A05E;
  43.         T_Unimplemented = $A89F;
  44.  
  45.     type
  46.         NMRecPtrPtr = ^NMRecPtr;
  47.         booleanPtr = ^boolean;
  48.  
  49.     var
  50.         current_note: NMRecPtr;
  51.  
  52. { handles must be non-purgeable, but may be unlocked }
  53.  
  54. {$S Init}
  55.     procedure InitNotify;
  56.     begin
  57.         current_note := nil;
  58.         notify_finished := false;
  59.         notify_outstanding := false;
  60.         time_to_unnotify := maxLongInt;
  61.     end;
  62.  
  63. {$S}
  64.     procedure MyResponse (note: NMRecPtr);
  65.     begin
  66.         booleanPtr(note^.nmRefCon)^ := true;
  67.     end;
  68.  
  69. {$S Util}
  70.     procedure UnNotify;
  71.         var
  72.             oe: OSErr;
  73.     begin
  74.         if current_note <> nil then begin
  75.             oe := NMRemove(current_note);
  76.             with current_note^ do begin
  77.                 if nmStr <> nil then
  78.                     DisposPtr(pointer(nmStr));
  79.                 if nmIcon <> nil then
  80.                     DisposHandle(nmIcon);
  81.             end;
  82.             DisposPtr(pointer(current_note));
  83.             current_note := nil;
  84.         end;
  85.         notify_finished := false;
  86.         notify_outstanding := false;
  87.         time_to_unnotify := maxLongInt;
  88.     end;
  89.  
  90. {$S}
  91.     procedure NotifyIdle (foreground: boolean);
  92.     begin
  93.         if (notify_finished and foreground) or (TickCount > time_to_unnotify) then
  94.             UnNotify;
  95.     end;
  96.  
  97. {$S Term}
  98.     procedure FinishNotify;
  99.     begin
  100.         if current_note <> nil then
  101.             UnNotify;
  102.     end;
  103.  
  104. {$S Util}
  105.     procedure NotifyH (mark: integer; sound: handle; sicn: handle; str: stringPtr; display_time: longInt);
  106.         var
  107.             error: boolean;
  108.             oe: OSErr;
  109.     begin
  110.         UnNotify;            { Clear outstanding notify }
  111.         if NGetTrapAddress(T_NMInstall, OSTrap) = NGetTrapAddress(T_Unimplemented, ToolTrap) then begin
  112.             SysBeep(1);   { Best we can do I guess.  Could put up the dialog box maybe?...}
  113.         end
  114.         else begin
  115.             current_note := NMRecPtr(NewPtr(sizeof(NMRec)));
  116.             if current_note = nil then begin
  117.                 SysBeep(1);   { Can't do much else if there isnt even room for this! }
  118.             end
  119.             else begin
  120.                 with current_note^ do begin
  121.                     qType := nmType;
  122.                     error := false;
  123.                     booleanPtr(nmRefCon) := @notify_finished;
  124.                     nmMark := mark;
  125.                     nmStr := str;
  126.                     nmIcon := sicn;
  127.                     nmSound := sound;
  128.                     nmResp := @MyResponse;
  129.                 end;
  130.                 oe := NMInstall(current_note);
  131.                 if oe <> noErr then begin
  132.                     current_note := nil;
  133.                     SysBeep(1);
  134.                 end
  135.                 else begin
  136.                     notify_outstanding := true;
  137.                     if display_time > 0 then
  138.                         time_to_unnotify := TickCount + display_time;
  139.                 end;
  140.             end;
  141.         end;
  142.     end;
  143.  
  144. {$S Util}
  145.     procedure Notify (mark, sound: boolean; sicn_id, sicn_index, str_id, str_index: integer; display_time: longInt);
  146.         var
  147.             errorText: str255;
  148.             sh: stringHandle;
  149.             sicnH: handle;
  150.             error: boolean;
  151.             nmMark: integer;
  152.             nmStr: stringPtr;
  153.             nmIcon: handle;
  154.             nmSound: handle;
  155.             gv: longInt;
  156.     begin
  157.         error := false;
  158.         if mark then
  159.             nmMark := 1
  160.         else
  161.             nmMark := 0;
  162.         nmStr := nil;
  163.         if str_id <> 0 then begin
  164.             if str_index > 0 then
  165.                 GetIndString(errorText, str_id, str_index)
  166.             else begin
  167.                 errorText := '';
  168.                 sh := GetString(str_id);
  169.                 if sh <> nil then begin
  170.                     if sh^ <> nil then
  171.                         errorText := sh^^;
  172.                     ReleaseResource(handle(sh));
  173.                 end;
  174.             end;
  175.             if errorText = '' then
  176.                 error := true
  177.             else begin
  178.                 nmStr := stringPtr(NewPtr(length(errorText) + 1));
  179.                 if nmStr = nil then
  180.                     error := true
  181.                 else
  182.                     nmStr^ := errorText;
  183.             end;
  184.         end;
  185.         nmIcon := nil;
  186.         if sicn_id <> 0 then begin
  187.  
  188.             nmIcon := nil;
  189.             if (Gestalt(gestaltSystemVersion, gv) = noErr) & (gv >= $0700) then begin
  190.                 if GetIconSuite(nmIcon, sicn_id, svAllSmallData) <> noErr then begin
  191.                     nmIcon := nil;
  192.                 end;
  193.             end;
  194.             if nmIcon = nil then begin
  195.                 if sicn_index < 1 then
  196.                     sicn_index := 1;
  197.                 sicn_index := (sicn_index - 1) * sicn_size;   { 1-based, like STR# }
  198.                 sicnH := GetResource('SICN', sicn_id);
  199.                 HNoPurge(sicnH);
  200.                 if sicnH = nil then
  201.                     error := true
  202.                 else begin
  203.                     nmIcon := NewHandle(sicn_size);
  204.                     if nmIcon = nil then
  205.                         error := true
  206.                     else if nmIcon^ = nil then
  207.                         error := true
  208.                     else if GetHandleSize(sicnH) < sicn_index + sicn_size then
  209.                         error := true
  210.                     else begin
  211.                         BlockMove(ptr(longInt(sicnH^) + sicn_index), nmIcon^, sicn_size);
  212.                     end;
  213.                     ReleaseResource(sicnH);
  214.                 end;
  215.             end;
  216.         end;
  217.         if sound or error then
  218.             nmSound := handle(-1)
  219.         else
  220.             nmSound := nil;
  221.         NotifyH(nmMark, nmSound, nmIcon, nmStr, display_time);
  222.     end;
  223.  
  224. end.